import Vue from 'vue'
import Vuex from 'vuex'
import {login} from '@/api/myApi'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    name:'',
    user:null
  },
  getters: {
  },
  mutations: {
    SET_NAME:(state,name)=>{
      state.name=name
    },
    SET_USER:(state,user)=>{
      state.user=user
    }
  },
  actions: {
    login({ commit },userinfo){
return new Promise((resolve,reject)=>{
  login(userinfo).then(response=>{
    // console.log(response)
    if(response.data.code==200){
const {username}=response.data.result
commit('SET_NAME',username)
commit('SET_USER',response.data.result)
    }
    else{
      reject(response.data.message)
    }
    resolve()
  }).catch(error=>{
    reject(error)
  })
})
    },
    loginOut({commit}){
      commit('SET_NAME','')
commit('SET_USER','')
    }
  },
  modules: {
  }
})
